﻿namespace MXERP.Blazor.Interop;

public class OutsideClickJsInterop
{
    private IOutsideEventJsCallback? _owner;
    private readonly IJSRuntime _jsRuntime;

    public OutsideClickJsInterop(IJSRuntime jsRuntime)
    {
        _jsRuntime = jsRuntime;
    }

    public ValueTask<bool> Initialize(IOutsideEventJsCallback owner, ElementReference element, Guid id)
    {
        _owner = owner;
        return _jsRuntime.InvokeVoidAsyncWithErrorHandling("OutsideClick.initialize", DotNetObjectReference.Create(this), element, id);
    }
    [JSInvokable]
    public async Task OnOutsideClick()
    {
        if (_owner == null) return;
        await _owner.HandleOnOutsideEventAsync();
    }
}